﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using spk_eAuction.Classes;
using System.Web.Security;
namespace spk_eAuction.admin
{
    public partial class ManageNews : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            /*
            if (Request.QueryString["cmd"] != null)
            {
                string cmd = Request.QueryString["cmd"].ToString();
                string param = Request.QueryString["param"].ToString();
                DetailsView DetailsView1 = null;

                switch (cmd)
                {
                    case "_view":
                    case "_edit":
                    case "_insert":                        

                        SqlDataSource2.SelectParameters["NewsID"].DefaultValue = param;

                        foreach (GridViewRow row in GridView1.Rows)
                        {
                            if (row.RowType == DataControlRowType.EmptyDataRow)
                            {
                                DetailsView1 = row.FindControl("DetailsView1") as DetailsView;
                                if (DetailsView1 != null) break;
                            }

                        }
                        if (DetailsView1 == null)
                            return;

                        DetailsView1.DataBind();
                        break;
                }
                switch (cmd)
                {
                    case "_view":
                        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
                        break;
                    case "_edit":
                        DetailsView1.ChangeMode(DetailsViewMode.Edit);
                        break;
                    case "_insert":
                        DetailsView1.ChangeMode(DetailsViewMode.Insert);
                        DropDownList DropDownList1 = DetailsView1.FindControl("DropDownList1") as DropDownList;
                        Label Label6 = DetailsView1.FindControl("Label6") as Label;

                        if (Label6 != null)
                            Label6.Text = DateTime.Now.ToString();
                        if (DropDownList1 != null)
                            DropDownList1.SelectedValue = Membership.GetUser().ProviderUserKey.ToString();
                        break;
                }
            } */
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            /*switch (e.CommandName)
            {
                case "_view":
                case "_edit":
                case "_insert":
                    GridView1.DataSourceID = "";
                    GridView1.DataBind();
                    
                    Response.Redirect(String.Format("~/Admin/ManageNews.aspx?cmd={0}&param={1}", e.CommandName, e.CommandArgument == null ? "" : e.CommandArgument));

                    break;
            }*/
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            TableCell cellDetails = e.Row.Cells[e.Row.Cells.Count - 1];
            Label Label1 = cellDetails.FindControl("Label1") as Label;
            if (Label1 != null)
            {

                Label1.Text = Label1.ToolTip = TextConvertor.Html2Text(Label1.Text);
                if (Label1.Text.Length > 40)
                    Label1.Text = Label1.Text.Substring(0, 20) + "...";
            }
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Image Image2 = DetailsView1.FindControl("Image2") as Image;
            TextBox TextBox2 = DetailsView1.FindControl("TextBox2") as TextBox;
            if (Image2 != null && TextBox2 != null)
            {
                Image2.ImageUrl = TextBox2.Text;
            }
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            Image Image3 = DetailsView1.FindControl("Image3") as Image;
            TextBox TextBox3 = DetailsView1.FindControl("TextBox3") as TextBox;
            if (Image3 != null && TextBox3 != null)
            {
                Image3.ImageUrl = TextBox3.Text;
            }
        }
        
        void GridViewDataBind()
        {
            MultiView1.SetActiveView(View1);
            GridView1.DataBind();
        }

        protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "_cancel":
                case "Cancel":
                    GridViewDataBind();
                    break;
            }
        }

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            GridViewDataBind();
        }

        protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            GridViewDataBind();
        }

        protected void ImageButtonDetail_Click(object sender, ImageClickEventArgs e)
        {
            MultiView1.SetActiveView(View2);
            SqlDataSource2.SelectParameters["NewsID"].DefaultValue = (sender as ImageButton).CommandArgument.ToString();
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
            DetailsView1.DataBind();
        }

        protected void ImageButtonEdit_Click(object sender, ImageClickEventArgs e)
        {
            MultiView1.SetActiveView(View2);
            SqlDataSource2.SelectParameters["NewsID"].DefaultValue = (sender as ImageButton).CommandArgument.ToString();
            DetailsView1.ChangeMode(DetailsViewMode.Edit);
            DetailsView1.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            MultiView1.SetActiveView(View2);
            SqlDataSource2.SelectParameters["NewsID"].DefaultValue = "";
            DetailsView1.ChangeMode(DetailsViewMode.Insert);
            DetailsView1.DataBind();

            DropDownList DropDownList1 = DetailsView1.FindControl("DropDownList1") as DropDownList;
            if (DropDownList1 != null)
            {
                DropDownList1.SelectedValue = Membership.GetUser().ProviderUserKey.ToString();
            }

            Label Label6 = DetailsView1.FindControl("Label6") as Label;
            if (Label6 != null)
            {
                Label6.Text = DateTime.Now.ToString();
            }
        }
    }
}